package com.jinlejia.erp.ui.mine.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.bean.SaleRecordBean;
import com.jinlejia.erp.databinding.AdapterMyrecordingItemBinding;
import com.jinlejia.erp.utils.StringUtil;
import com.jinlejia.oldmodule.databinding.OldCommonMp3ViewLayoutBinding;
import com.wang.avi.AVLoadingIndicatorView;
import com.weight.mp3.AudioController;
import com.weight.mp3.Mp3Helper;
import com.weight.mp3.Mp3HelperListener;
import com.weight.mp3.SoundBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author SongJiaTai
 * @description:录音列表Adapter
 * @date :2019/11/11 11:49
 */
public class RecordingListAdapter extends YfListAdapter<SaleRecordBean> implements Mp3HelperListener {
    private final Mp3Helper mp3Helper;
    private final RecyclerView recyclerView;
    public RecordingListAdapter(ArrayList<SaleRecordBean> data,AudioController audioController,RecyclerView recyclerView) {
        super(data);
        mp3Helper = new Mp3Helper(audioController,recyclerView,this);
        this.recyclerView = recyclerView;
    }


    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_myrecording_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }


    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }


    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }


    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((RecordingListAdapter.FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((RecordingListAdapter.FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((RecordingListAdapter.FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((RecordingListAdapter.FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((RecordingListAdapter.FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public SoundBean getMp3SoundBean(int position) {
        ArrayList<SaleRecordBean> list = mData;
        if (list!=null && list.size()>position){
            return list.get(position).getSoundContent();
        }
        return null;
    }


    @Override
    public OldCommonMp3ViewLayoutBinding getMp3Binding(int position) {
        RecyclerView.ViewHolder holder = recyclerView.findViewHolderForLayoutPosition(position);
        OldCommonMp3ViewLayoutBinding mp3Binding = null;
        if (holder instanceof ViewHolder){
            AdapterMyrecordingItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
            mp3Binding = binding.musicView.getBinding();
        }
        return mp3Binding;
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }


    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        AdapterMyrecordingItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setIsRecyclable(true);
        SaleRecordBean bean = mData.get(position);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        binding.followTime.setText(sdf.format(new Date(bean.getFollowTime())));
        binding.brokerInfo.setText(StringUtil.valueOf(bean.getUserName()) + "      " + StringUtil.valueOf(bean.getShareStoreName()));
        if ("3".equals(String.valueOf(bean.getFollowState()))){
            binding.type.setText("未保护");
            binding.btnProtect.setText("设为保护");
        }else {
            binding.type.setText("已保护");
            binding.btnProtect.setText("取消保护");
        }

        binding.phoneNo.setText(StringUtil.valueOf(bean.getCallingId()));
        SoundBean soundBean = bean.getSoundContent();
        mp3Helper.initMp3View(binding.musicView.getBinding(),soundBean,position);
        binding.callIcon.setOnClickListener(v -> {
            if (null != callListener) {
                callListener.onCall(bean.getCallingId());
            }
        });

        binding.btnProtect.setOnClickListener(v -> {
            SetAsProtection.onState(binding.type, binding.btnProtect, binding.btnProtect.getText().toString(), bean.getFollowId());
        });


    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }


    //region 拨打电话按钮回调
    public interface OnCallListener {
        void onCall(String tel);
    }

    OnCallListener callListener;

    public void setOnCallListener(OnCallListener listener) {
        this.callListener = listener;
    }
    //endregion


    // region 设为保护按钮回调
    public interface OnSetAsProtection {
        void onState(TextView tvType, Button btnProtect, String state, String followId);
    }

    OnSetAsProtection SetAsProtection;

    public void setOnSetAsProtecteionListener(OnSetAsProtection listener) {
        this.SetAsProtection = listener;
    }

    //endregion

}
